# some util method to handle a parsed_sentence construct

def getParts(partname, parsed_sentence):
    """
        return the parts that match the partname
        parsed_sentence = [['NP', ('Bob', 'NP', 'NNP')], ['VP', ('is', 'BEZ', 'VBZ')], ['NP', ('a', 'AT', 'DT'), ('kitchen', 'NN', 'NN')]]
        e.g.:
        vpParts = getParts('VP', parsed_sentence) # list of VP-parts
        npParts = getParts('NP', parsed_sentence) # list of NP-parts
        ppParts = getParts('PP', parsed_sentence) # list of PP-parts        
    """
    return filter(lambda x: x[0] == partname,parsed_sentence)


######################################################################3
# functions to determine special verbs (to be/have)


def isSpecialVerb(verb):
    """ Returns true if verb is "to be" (Currently only "was"/"is")
        verb: 'is' | 'has' | 'gone' """
    specialverbs = ['is','was']
    return len(filter(lambda x: x == verb[0], specialverbs)) > 0

def hasSpecialVerbs(vpPart):
    """ Returns true if vpPart has the verb "to be"  in it
        vpPart = ['VP', verb, verb] """
    return len(filter(lambda x: isSpecialVerb(x), vpPart[1:])) > 0

def sentenceHasSpecialVerb(parsed_sentence):
    """ Returns true if vpParts has vpPart in it with the verb "to be"
        vpParts = [['VP', 'was', 'gone'], ['VP', 'is', 'married'], ['VP', 'shoot'], ...] """
    #print map(hasSpecialVerbs, vpParts)
    vpParts = getParts('VP', parsed_sentence) # list of VP-parts
    return len(filter(lambda x: hasSpecialVerbs(x), vpParts)) > 0


def stripNoun(npPart):
    """
        removes the head of the list and any tuples that are adjectives, determiners, ...
        e.g.: the big red house => house
        npPart = ['NP', ('the', 'AT', 'DT'), ('kitchen', 'NN', 'NN')]
        returns [('kitchen', 'NN', 'NN')]
    """
    # assert npPart[0] == 'NP'
    n_tuples = npPart[1:] # skip the first item in the list

    tag_index = 2 # which part of the tuple should we check for the tag
    # only keep NNP,NN
    #print n_tuples # (word, brown_tag, connll_tag), (word, brown_tag, connll_tag), ...
    noun_words = filter(lambda x: isNoun(x, tag_index), n_tuples)
    #for t in n_tuples:
    #    print isNoun(t, tag_index)
    if len(noun_words) == 0:
        # no NN in this part
        # just remove the AT and keep all other parts
        no_dt_words = filter(lambda x: not isArticle(x, tag_index), n_tuples)
        return no_dt_words
    else:
        return noun_words

def getVerb(tagged_sentence):
    """ Returns the verb in the sentence
        tagged sentence is a list of tuples (word, tag, chunk)
        """
    return filter(isVerb, tagged_sentence)
    

    
def getNouns(tagged_sentence):
    """ Returns a list of nouns in the sentence
        tagged sentence is a list of tuples (word, tag, chunk)
        """
    return filter(isNoun, tagged_sentence)
    
    
def isArticle(tagged_word, tag_index):
    """ Returns true if tagged word is a Determiner (DT)
        tagged_words is a tuple (word, tag, chunk)
    """
    return tagged_word[1] == 'AT'
        
    
def isVerb(tagged_word):
    """ Returns true if tagged_word is a verb (VP)
        tagged_words is a tuple (word, tag, chunk)
        """
    # TODO add extra verb-tags
    return tagged_word[1] == 'VP' 

def isNoun(tagged_word, tag_index):
    """ Returns true if tagged_word is a Noun (NN)
        tagged_words is a tuple (word, tag, chunk)
        tag_index wich index of the tuple should we check for the tag
        """
    # TODO add extra noun-tags
    noun_list = ['NN', 'NNP']

    return (len(filter(lambda x: x == tagged_word[tag_index], noun_list)) == 1)
